import request from "@/utils/request"
export const getChannelAction = (payload) => {
  return async (dispatch, getState) => {
    const { data } = await request.get('/channels')
    // console.log('data数据', data.channels)

    dispatch({ type: 'article/channel', payload: data.channels })
  }
}
export const getArticleAction = (payload) => {
  return async (dispatch, getState) => {
    const { data: { page, per_page, results, total_count } } = await request.get('/mp/articles', {
      params: payload
    })
    const datas = {
      page,
      pageSize: per_page,
      list: results.map(item => {
        return {
          ...item,
          cover: item.cover.images[0]
        }
      }),
      total: total_count
    }
    console.log('data数据', datas)

    dispatch({ type: 'article/list', payload: datas })
  }
}
export const delArticleAction = (id, filters) => {
  return async (dispatch, getState) => {
    /**
     * 1.调用接口删除文章(数据库)
     * 2.重新获取文章列表
     */
    await request.delete(`/mp/articles/${id}`)
    dispatch(getArticleAction(filters))
    // dispatch({ type: '', payload })
  }
}
export const addArticleAction = (data, isDraft, isEdit) => {
  return async (dispatch, getState) => {
    // 区分：1.发布文章(新增) 2.编辑(更新)
    if (isEdit) {
      await request.put(`/mp/articles/${data.id}?draft=${isDraft}`, data)
    } else {
      // 后期区分是否是草稿：1.发布文章(不是草稿) 2.存入草稿(draft = true)
      await request.post(`/mp/articles?draft=${isDraft}`, data)
    }
  }
}